//
//  ShoppingCartAnimationTool.m
//  xfb
//
//  Created by XFB on 2022/6/28.
//  Copyright © 2022 XFB. All rights reserved.
//

#import "ShoppingCartAnimationTool.h"

@interface ShoppingCartAnimationTool() <CAAnimationDelegate>

@end

@implementation ShoppingCartAnimationTool

// MARK: - instancetype
+ (instancetype)sharedTool {
    return [[ShoppingCartAnimationTool alloc] init];
}

// MARK: - public function
- (void)startAnimationandView:(UIView *)view
                         rect:(CGRect)rect
                  finisnPoint:(CGPoint)finishPoint
                  finishBlock:(AnimationFinishBlock)completion {
    // 图层
    self.layer = [CALayer layer];
    self.layer.contents = view.layer.contents;
    self.layer.contentsGravity = kCAGravityResizeAspectFill;
    self.layer.bounds = rect;
    self.layer.cornerRadius  = rect.size.width/2;
    self.layer.masksToBounds = YES;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow.layer addSublayer:self.layer];
    self.layer.position = CGPointMake(rect.origin.x+view.frame.size.width/2, CGRectGetMidY(rect));
    // 路径
    [self createAnimationwithRect:rect finishPoint:finishPoint];
    // 回调
    if (completion) {
        self.animationFinishBlock = completion;
    }
}

// MARK: - private function
// 创建动画
- (void)createAnimationwithRect:(CGRect)rect
                    finishPoint:(CGPoint)finishPoint {
    // 屏幕宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    
    // 路径动画
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:self.layer.position];
    [path addQuadCurveToPoint:finishPoint controlPoint:CGPointMake(screenW/2, rect.origin.y-80)];
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.path = path.CGPath;
    // 旋转动画
    CABasicAnimation *rotateAnimation   = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotateAnimation.removedOnCompletion = YES;
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0];
    rotateAnimation.toValue   = [NSNumber numberWithFloat:12];
    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    // 新增缩放动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:0.25];
    // 添加动画动画组合
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[pathAnimation, rotateAnimation, scaleAnimation];
    groups.duration = 1.2f;
    groups.removedOnCompletion=NO;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    [self.layer addAnimation:groups forKey:@"group"];
}

// MARK: - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (anim == [self.layer animationForKey:@"group"]) {
        [self.layer removeFromSuperlayer];
        self.layer = nil;
        if (self.animationFinishBlock) {
            self.animationFinishBlock(YES);
        }
    }
}

// MARK: - dealloc
- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end

